package com.cskaoyan;

import com.cskaoyan.connectionpool.MyConnectionPoolV1;
import com.cskaoyan.connectionpool.MyConnectionPoolV3;
import com.cskaoyan.connectionpool.opensource.C3p0Utils;
import com.cskaoyan.connectionpool.opensource.C3p0Utils2;
import com.cskaoyan.connectionpool.opensource.DBCPUtils;
import com.cskaoyan.connectionpool.opensource.DruidUtils;
import com.cskaoyan.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 创建日期: 2022/08/26 16:27
 *
 * @author ciggar
 */
public class DatasourceDemo {

    public static void main(String[] args) throws SQLException {

        // 1. 从连接池获取连接
        Connection connection = MyConnectionPoolV3.getConnection();  // JDBC4Connection
//        Connection connection2 = DBCPUtils.getConnection();
        // org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
        // connection2.close();

        // String className = connection2.getClass().getName();
        // System.out.println(className);

        Connection connection3 = C3p0Utils2.getConnection();

        Connection connection4 = DruidUtils.getConnection();


        // 2. 创建Statement
        Statement statement = connection.createStatement();


        // 3. 执行SQL语句
        ResultSet resultSet = statement.executeQuery("select * from account");


        // 4. 解析结果集
        while (resultSet.next()) {

            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int money = resultSet.getInt("money");

            System.out.println("id:" + id + ",name:" + name + ",money:" + money);
        }

        // 5. 关闭资源
        JDBCUtils.close(resultSet,statement,null);

        connection.close();


    }
}
